import { apiCity } from 'js/api'
import { storage } from 'js/util'

export default {
  state: {
    provinceCityList: storage.get('provinceCityList') || []
  },
  getters: {
    provinceCityList: state => state.provinceCityList
  },
  actions: {
    /**
     * 获取 省市区
     */
    fetchProvCity({ state }) {
      if (storage.get('provinceCityList')) return
      apiCity().then(data => {
        const list = data.data || {}
        if (list && list.length == 0) return
        state.provinceCityList = list.map(item => {
          return {
            value: item.id,
            label: item.areaName,
            child: item.childs
          }
        })
        storage.set('provinceCityList', state.provinceCityList)
      })
    }
  }
}